home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk4 / acron24a / timer.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  4KB  |  158 lines

  1. /* SIMPLE TIMER EXAMPLE PROGRAM: 
  2.  *
  3.  * Includes dynamic allocation of data structures needed to communicate
  4.  * with the timer device as well as the actual device IO
  5.  *
  6.  * Author:  Rob Peck, 12/1/85 */
  7.  
  8. /* Stripped to TimeDelay & supporting functions for Cron inclusion <CB> */
  9.  
  10. #include "exec/types.h"
  11. #include "exec/nodes.h"
  12. #include "exec/lists.h"
  13. #include "exec/memory.h"
  14. #include "exec/interrupts.h"
  15. #include "exec/ports.h"
  16. #include "exec/libraries.h"
  17. #include "exec/io.h"
  18. #include "exec/tasks.h"
  19. #include "exec/execbase.h"
  20. #include "exec/devices.h"
  21. #include "devices/timer.h"
  22.  
  23. long TimerBase;        /* to get at the time comparison functions */
  24.  
  25.  
  26. /* *********************************************************************** */
  27. /*    Timer function - timedelay(seconds,microseconds)
  28.  
  29.     Your task is put to sleep for the specified time interval.
  30.  
  31.     If seconds > 0, then UNIT_VBLANK is used, delay is in multiples of
  32.     60ths of a second.  If seconds < 0, then UNIT_MICROHZ is used for 
  33.     more precision.    
  34.  
  35.     Returns value of 0 if no errors, nonzero (and no task sleeping)
  36.  
  37.     Notice that since this is a multi-tasking system, the delays
  38.     shown here must be considered to be only approximate.
  39.  
  40.     Also note that this function is used primarily to show how
  41.     a timer device is accessed, including the creation of the 
  42.     message port and a message structure (IOStdReq).   Note that
  43.     there is a Delay(interval) function already in the DOS.library.
  44.     (See the DOS developer's manual for details).
  45.     
  46.     But this Delay() function leaves LOT to be desired, <CB>.
  47. */
  48. /* *********************************************************************** */
  49.  
  50. extern struct MsgPort *CreatePort();
  51. extern struct IORequest *CreateExtIO();
  52.  
  53. struct timerequest 
  54. *PrepareTimer(precision)
  55. SHORT precision;
  56. {
  57.     /* return a pointer to a time request.  If any problem, return NULL */
  58.  
  59.     int error;
  60.     SHORT whichunit;
  61.  
  62.     struct MsgPort *timerport;
  63.     struct timerequest *timermsg;
  64.     
  65.         timerport = CreatePort(0,0);
  66.         if (timerport == NULL) 
  67.         return(NULL);    /* Error during CreatePort */
  68.  
  69.     timermsg = (struct timerequest *)CreateExtIO(
  70.                     timerport,sizeof(struct timerequest));
  71.     if (timermsg == NULL)
  72.         {
  73.         DeletePort(timerport);
  74.         return(NULL);    /* Error during CreateExtIO */
  75.         }
  76.     
  77.     if(precision)    /* if true, use precision timer  ( under 1 second ) */
  78.         whichunit = UNIT_MICROHZ;
  79.     else
  80.         whichunit = UNIT_VBLANK;
  81.  
  82.     error = OpenDevice(TIMERNAME, whichunit, timermsg, 0);
  83.     if (error != 0)
  84.         {
  85.         DeleteExtIO(timermsg,sizeof(struct timerequest));
  86.         DeletePort(timerport);
  87.         return(NULL);    /* Error during OpenDevice */
  88.         }
  89.     return(timermsg);
  90. }
  91.  
  92. int
  93. GetTimerBase()
  94. {
  95.     int tbase;
  96.     struct timerequest *tr;
  97.     tr = PrepareTimer();
  98.     tbase = (int)tr->tr_node.io_Device;        
  99.     DeleteTimer(tr);
  100.     return(tbase);
  101. }
  102.  
  103. int 
  104. TimeDelay(seconds,microseconds,precision)
  105.         /* more precise timer than AmigaDOS Delay() */
  106. ULONG seconds,microseconds;
  107. int precision;
  108. {
  109.     int precise;
  110.     struct timerequest *tr;
  111.     if(seconds < 0 || precision != 0)
  112.                 /* do delay in terms of microseconds */ 
  113.         precise = TRUE; /* yes, use the precision timer.     */
  114.     else 
  115.         precise = FALSE; /* no, not necessary */
  116.  
  117.     tr = PrepareTimer(precise);
  118.                     /* get a pointer to an initialized
  119.                      * timer request block */
  120.     if(tr == NULL) return(-1);    /* any nonzero return says timedelay
  121.                      * routine didn't work. */
  122.     WaitForTimer(tr,seconds,microseconds);
  123.  
  124.     DeleteTimer(tr);    /* deallocate temporary structures */
  125.     return(0);
  126. }            /* end of timedelay */
  127.  
  128.  
  129. int
  130. WaitForTimer(tr,seconds,microseconds)
  131. ULONG seconds,microseconds;
  132. struct timerequest *tr;
  133. {
  134.     tr->tr_node.io_Command = TR_ADDREQUEST;   /* add a new timer request */
  135.         tr->tr_time.tv_secs =  seconds;            /* seconds */
  136.         tr->tr_time.tv_micro = microseconds;     /* microseconds */
  137.         DoIO( tr );                     /* post request to the timer */
  138.                         /* goes to sleep till done */
  139.     return(0);
  140. }
  141.  
  142. int
  143. DeleteTimer(tr)
  144. struct timerequest *tr;
  145. {
  146.     struct MsgPort *tp;
  147.  
  148.     tp = tr->tr_node.io_Message.mn_ReplyPort;
  149.     if(tr != 0)
  150.     {
  151.         CloseDevice(tr);
  152.         DeleteExtIO(tr,sizeof(struct timerequest));
  153.     }
  154.     if(tp != 0)
  155.         DeletePort(tp);
  156.     return(0);        
  157. }
  158.